package com.zhw.controller;

import com.alibaba.fastjson.JSONObject;
import com.zhw.Util.JwtUtils;
import com.zhw.pojo.Emp;
import com.zhw.pojo.Result;
import com.zhw.pojo.User;
import com.zhw.service.EmpService;
import com.zhw.service.UserService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginController {

/*    @Autowired
    EmpService es;*/

    @Autowired
    UserService userService;

    @PostMapping("/login")
    public Result login(@RequestBody User user){
        User u=userService.login(user);
        if (u!=null){
            Map<String,Object> m=new HashMap();
            m.put("username",u.getUsername());
            m.put("password",u.getPassword());
            String s = JwtUtils.generateJwt(m);
            return Result.success(s);
        }
        return Result.error("输入的用户名或密码有错误");
    }


/*    @PostMapping("/login")
    public Result Login(@RequestBody Emp emp){
        Emp e=es.login(emp);
        if (e!=null){
//            登录成功，创建令牌
            Map<String, Object> map=new HashMap<>();
            map.put("id",e.getId());
            map.put("username",e.getUsername());
            map.put("password",e.getPassword());
            String s = JwtUtils.generateJwt(map);
            return Result.success(s);

        }


        return e!=null?Result.success("登陆成功"):Result.error("输入的用户名或密码有错误");
    }*/
}
